package 集合学习.jihe_Set.Setjihewanzheng;

import java.util.*;
import java.lang.*;

/**
 *
 * HashSet LinkedHashSet存储普通的数据类型是保证唯一的，如果是自定义的数据类型不重写equals()和HashCode()方法就不能保证唯一性
 * 因此在需要重写equals()和HashCode()方法两者缺一不可
 * TreeSet需要进行实现Comparable接口
 *
 * */
public class Text_Set {

    public static void main(String[] args) {

       // Comparator ex=new 外部比较器();
       // Set<Student> set0 =new TreeSet<>(ex);//指定了外部比较器，那就是外部比较器优先

        Comparator comparator=new Comparator<Student>() {//直接把外部比较器放到内部
            @Override
            public int compare(Student stu1,Student stu2) {
                if(stu1.getScore()>stu2.getScore())
                    return 1;
                else if(stu1.getScore()<stu2.getScore())
                    return -1;
                else return stu1.getName().compareTo(stu2.getName());
            }
        };

//        Set<Student> un =new TreeSet<Student>((stu1,stu2)->{return stu1.getName().compareTo(stu2.getName());});
//        Set<Student> un1 =new TreeSet<Student>((stu1,stu2)->stu1.getName().compareTo(stu2.getName()));
  //上面两种为lambda表达式形式
        Set<Student> set =new HashSet<>();
        Set<Student>set1 =new LinkedHashSet<>();
        Set<Student>set2 =new TreeSet<>();
        //使用set存储多个学生信息
        Student student=new Student("zhangsan","nan",23,98);
        Student student1=new Student("lisi","nan",24,87);
        Student student2=new Student("wangwu","nan",23,98);
        Student student3=new Student("zhangsan","nan",23,98);

        set.add(student); set1.add(student); set2.add(student); //set0.add(student);
        set.add(student1); set1.add(student1); set2.add(student1); //set0.add(student1);
        set.add(student2); set1.add(student2); set2.add(student2); //set0.add(student2);
        set.add(student3); set1.add(student3); set2.add(student3); //set0.add(student3);

        //遍历集合
        System.out.println(set.size());
        for(Student e:set){
            System.out.println(e);
        }
        System.out.println(set1.size());
        for(Student e:set1){
            System.out.println(e);
        }
        System.out.println(set2.size());
        for(Student e:set2){
            System.out.println(e);
        }

//        System.out.println(set0.size());
//        for(Student e:set0){
//            System.out.println(e);
//        }

        //
        Set<Student> set4=new TreeSet<Student>((stu1,stu2)->{if(stu1.getScore()>stu2.getScore())
            return 1;
        else if(stu1.getScore()<stu2.getScore())
            return -1;
        else return stu1.getName().compareTo(stu2.getName());});

    }
}
